package com.lzb.browser.activity;

import android.os.Bundle;
import android.view.View;

import com.lzb.browser.BaseAc;
import com.lzb.browser.Content;
import com.lzb.browser.R;
import com.lzb.browser.basic.utils.SPUtil;
import com.lzb.browser.databinding.ActivityBrowserIdentificationBinding;
import com.lzb.browser.fragment.BrowserFragment;

public class BrowserIdentificationActivity extends BaseAc<ActivityBrowserIdentificationBinding> implements View.OnClickListener {


    private String currentUaMode;

    @Override
    protected int onCreate() {
        return R.layout.activity_browser_identification;
    }

    @Override
    protected void initView() {
        mDataBinding.ivBrowserIdentificationBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.llAndroid.setOnClickListener(this);
        mDataBinding.llComputer.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.llAndroid) {//Android
            switchUaMode(getString(R.string.android));
        } else if (id == R.id.llComputer) {//电脑
            switchUaMode(getString(R.string.computer));
        }
    }

    @Override
    protected void initData() {
        currentUaMode = SPUtil.getString(this, Content.browseIdentification, getString(R.string.android));
        updateUiState();
    }

    /**
     * 更新UI状态
     */
    private void updateUiState() {
        if (getString(R.string.android).equals(currentUaMode)) {
            // Android模式选中
            mDataBinding.tvAndroid.setTextColor(0xFFBF96FF); // #BF96FF
            mDataBinding.ivAndroid.setVisibility(View.VISIBLE);

            // PC模式未选中
            mDataBinding.tvComputer.setTextColor(0xFF333333); // #333333
            mDataBinding.ivComputer.setVisibility(View.GONE);
        } else {
            // Android模式未选中
            mDataBinding.tvAndroid.setTextColor(0xFF333333); // #333333
            mDataBinding.ivAndroid.setVisibility(View.GONE);

            // PC模式选中
            mDataBinding.tvComputer.setTextColor(0xFFBF96FF); // #BF96FF
            mDataBinding.ivComputer.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 切换UA模式
     */
    private void switchUaMode(String mode) {
        if (!mode.equals(currentUaMode)) {
            currentUaMode = mode;
            // 保存到SharedPreferences
            SPUtil.putString(this, Content.browseIdentification, currentUaMode);
            SPUtil.putString(this, Content.uaCode, getUserAgentString());
            // 更新UI
            updateUiState();
        }
    }

    /**
     * 获取当前UA字符串
     */
    private String getUserAgentString() {
        if (getString(R.string.android).equals(currentUaMode)) {
            return "Mozilla/5.0 (Linux; Android 10; SM-G973F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.120 Mobile Safari/537.36";
        } else if (getString(R.string.computer).equals(currentUaMode)) {
            return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36";
        }
        return "";
    }
}